package com.bw.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.domain.User;
import com.bw.service.UserService;
import com.bw.utils.common.JWTUtils;
import com.bw.utils.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

@RestController
@RequestMapping("auth")
public class LoginController {

    @Autowired
    UserService userService;

    @RequestMapping("login")
    public Result login(@RequestBody User user){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        User one = userService.getOne(wrapper);
        if (one==null){
            return Result.fail("用户名不存在");
        }
        String s = DigestUtil.md5Hex(user.getPassword() + salt);
        if (!one.getPassword().equals(s)){
            return Result.fail("密码不存在");
        }
        String token = JWTUtils.createToken(user.getUsername(), 60 * 12);
        return Result.success(token);
    }

    public static final  String salt="2109a";
    @PostConstruct
    public void md5(){
        String pwd="123";
        String s = DigestUtil.md5Hex(pwd + salt);
        System.out.println(s);
    }
}
